/**
  ******************************************************************************
  * @file    Project/USER/comm.c 
  * @author  liuyi
  * @date    2017/8/15
  * @brief   Communicate with another board program body
   ******************************************************************************
   */
   
/* Includes ------------------------------------------------------------------*/
#include "comm.h"
#include "usart.h"
#include "log.h"

uint8_t MsgBuf[MSG_BUF_LEN];

void UsartSendMsg(uint8_t * Buf,	uint8_t Length)
{
	MsgCommunicationSturctType * Msg;
	
	//if(NULL == Buf)
	//	return;

	Msg = (MsgCommunicationSturctType *)Buf;

	Msg->Head.Length	= Length;
	Msg->Head.Start		= 0XFFFF;
	Msg->Head.CRCCheck		= CRCCheck(Buf + 4, Length - 6);
	
	Buf[Length - 2] = 0xFE;
	Buf[Length - 1] = 0xFE;
#ifdef LOG_INFO

	uint8_t Length_i = 0;
	LOG_INFO("Send Buf:");
	for(; Length_i < Length; Length_i++)
	printf(" %x", Buf[Length_i]);
	printf("\r\n");
#endif 
	Usart3SendData_DMA(Buf, Length);

}


uint16_t CRCCheck(uint8_t * Buf, uint8_t Length)
{
	uint8_t i = 0;
	uint16_t CRCCheck = 0;

	for(; i < Length; i++)
	{
		CRCCheck += Buf[i];
	}

	return ((CRCCheck & 0x00ff) << 8) |((CRCCheck & 0xff00) >> 8);
}	

